import { login, logout, smsLogin,getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    path:'',
    roles: [],
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_PATH: (state, path) => {
    state.path = path
  },
  SET_ROLE: (state, roles) => {
    state.roles =roles
  },
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    //和下面的smsLogin  一样
    //只是参数列表不同
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      //login请求可以直接跳转
      //如果报黄线可以在最上面导入这个方法
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data)
        setToken(data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  //smsLogin  验证码登录
  smsLogin({ commit }, userInfo) {
    //和下面的smsLogin  一样
    //只是参数列表不同
    //参数改变
    const { phone, code,encode } = userInfo
    return new Promise((resolve, reject) => {
      //smsLogin请求可以直接跳转
      //如果报黄线可以在最上面导入这个方法
      smsLogin({ phone: phone.trim(), code: code,encode: encode }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data)
        setToken(data)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },


  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        // console.log(data);
        //username = data.username
        const { username, avatar,path,roleList } = data.list[0]
        var roles=[]
        for (const Key in roleList) {
          roles.push(roleList[Key].name)
        }



        commit('SET_NAME', username)
        commit('SET_AVATAR', avatar)
        commit('SET_PATH', path)
        commit('SET_ROLE', roles)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

